document.addEventListener('DOMContentLoaded', function() {
    console.log('Popup loaded'); // 调试日志
    
    // 检查存储API是否可用
    if (!chrome.storage) {
        showError('存储API不可用');
        return;
    }
    
    chrome.storage.local.get(['sourceDomain', 'targetDomain'], function(result) {
        if (chrome.runtime.lastError) {
            console.error('Storage error:', chrome.runtime.lastError);
            showError('读取设置失败: ' + chrome.runtime.lastError.message);
            return;
        }
        
        console.log('Loaded settings:', result); // 调试日志
        
        const sourceInput = document.getElementById('sourceDomain');
        const targetInput = document.getElementById('targetDomain');
        const currentSource = document.getElementById('currentSource');
        const currentTarget = document.getElementById('currentTarget');
        
        // 总是设置默认值，即使存储中有值也用作后备
        const defaultSource = 'github.com';
        const defaultTarget = 'bgithub.xyz';
        
        if (result.sourceDomain) {
            sourceInput.value = result.sourceDomain;
            currentSource.textContent = result.sourceDomain;
        } else {
            sourceInput.value = defaultSource;
            currentSource.textContent = defaultSource;
        }
        
        if (result.targetDomain) {
            targetInput.value = result.targetDomain;
            currentTarget.textContent = result.targetDomain;
        } else {
            targetInput.value = defaultTarget;
            currentTarget.textContent = defaultTarget;
        }
    });
    
    document.getElementById('saveBtn').addEventListener('click', saveSettings);
});

function saveSettings() {
    const sourceDomain = document.getElementById('sourceDomain').value.trim();
    const targetDomain = document.getElementById('targetDomain').value.trim();
    const statusElement = document.getElementById('status');
    
    if (!sourceDomain || !targetDomain) {
        showError('错误: 源域名和目标域名都不能为空');
        return;
    }
    
    chrome.storage.local.set({
        sourceDomain: sourceDomain,
        targetDomain: targetDomain
    }, function() {
        if (chrome.runtime.lastError) {
            console.error('Save error:', chrome.runtime.lastError);
            showError('保存失败: ' + chrome.runtime.lastError.message);
            return;
        }
        
        document.getElementById('currentSource').textContent = sourceDomain;
        document.getElementById('currentTarget').textContent = targetDomain;
        
        showSuccess('设置已保存，立即生效！');
        
        setTimeout(function() {
            statusElement.className = 'status';
        }, 3000);
        
        // 发送消息通知background script更新规则
        chrome.runtime.sendMessage({action: 'settingsUpdated'}, function(response) {
            if (chrome.runtime.lastError) {
                // 这里可能会有错误，因为没有响应，这是正常的
                console.log('Message sent (may be normal if no listener):', chrome.runtime.lastError);
            }
        });
    });
}

function showError(message) {
    const statusElement = document.getElementById('status');
    statusElement.textContent = message;
    statusElement.className = 'status error';
}

function showSuccess(message) {
    const statusElement = document.getElementById('status');
    statusElement.textContent = message;
    statusElement.className = 'status success';
}